package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Course;
import com.pojo.Score;
import com.service.CourseService;
import com.service.ScoreService;
import com.utils.ApiResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author
 * @description score
 * @date 2022-12-19
 */
@RestController
@RequestMapping(value = "/score")
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @Resource
    private CourseService courseService;

    /**
     * 新增
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/insert")
    public ApiResult insert(@RequestBody Score score) {
        Course course = (Course) courseService.queryByCourseName(score.getCourseName());
        score.setCourseTeacher(course.getTeacher());
        return new ApiResult(200, "新增成功", scoreService.insert(score));
    }

    /**
     * 刪除
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/delete")
    public ApiResult delete(int id) {
        int result = (int) scoreService.delete(id);
        return new ApiResult(200, "success", result);
    }

    /**
     * 更新
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/update")
    public ApiResult update(@RequestBody Score score) {
        return new ApiResult(200, "success", scoreService.update(score));
    }


    /**
     * 查询 分页查询
     *
     * @author
     * @date 2022/12/19
     **/
    @RequestMapping("/pageList")
    public ApiResult pageList(@RequestParam(required = false, defaultValue = "1") int page,
                              @RequestParam(required = false, defaultValue = "5") int limit
    ) {
        // mybatis分页插件
        PageHelper.startPage(page, limit);
        List<Score> scores = scoreService.pageList(page, limit);
        // 取分页信息
        PageInfo<Score> pageInfo = new PageInfo<Score>(scores);
        return new ApiResult(0, "success", (int) pageInfo.getTotal(), pageInfo.getList());
    }

}